<?php 
/**
 * 管理权限角色
 * @author Haiqin
 *
 */
namespace Users\Controller;
use Admin\Controller\AdminbaseController;
class AuthgroupadminController extends AdminbaseController{
	
	protected $m;
	
	function _initialize() {
		parent::_initialize();
		$this->m = M('AuthGroup');
	}
	
	/**
	 * @name 角色管理
	 * @auth adminleft
	 */
	public function index(){
		if(!$_SESSION['auth_role_group']){
			$_SESSION['auth_role_group'] = 'admin';
		}
		if($_GET['group']){
			$_SESSION['auth_role_group'] = $_GET['group'];
		}
		$this->auth_role_group = $_SESSION['auth_role_group']; //输出查询的分组
		
		$this->display();
	}
	
	public function edit(){
		if($_GET['id']){
			$this->info = $this->m->find($_GET['id']);
		}
		$this->display();
	}
	
	public function edit_post(){
		if(IS_POST){
			$_POST['update_time'] = date('Y-m-d H:i:s');
			$_POST['rules'] = implode(',',$_POST['rule']);
			$_POST['xiaoqu'] = implode(',',$_POST['xiaoqu']);
			$this->m->create();
			unset($_SESSION['curUserRole']); //删除掉当前用户的权限，以便重新获取更新后的权限
			if($_POST['id']){
				if($this->m->save()){
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}else{
				if($this->m->add()){
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
			}
		}
	}
	
	public function del(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array('id' => array('in', $id));
		if($this->m->where($map)->delete()){
			$this->success('删除成功',__URL__);
		} else {
			$this->error('删除失败！');
		}
	}
	
	//判断角色唯一
	public function checkauth(){
		$map['title'] = $_REQUEST['title'];
		$map['id'] = array('not in',$_REQUEST['id']);
		$auth = $this->m->where($map)->find();
		if($auth){
			echo  '{"error":"角色名已经存在"}';
		}else{
			echo '{"ok":"角色名可用"}';
		}
	}
}
?>